<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  Software_model extends CI_Model 
{
	/**
	 * @var unknown_type
	 */
	var $software_id  = 0;  
	/**
	 * @var unknown_type
	 */
	
	var $vi_name = null ; 
	
	var $en_name = null ; 
	/**
	 * @var unknown_type
	 */
	var $link_to = null ; 
	
	var $link_type = null ; 
	/**
	 * @var unknown_type
	 */
	var $position = 0 ; 
	/**
	 * @var unknown_type
	 */
	var $order_view = null ; 
	/**
	 * @var unknown_type
	 */
	var $active = 0;
	/*
	 * @access	public
	 */
	 var $image = '';
	/*
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
		
	}
		
	function insert(){
		$sql = "INSERT INTO gtb_software (vi_name, en_name, link_to, link_type, order_view, active, image)
				VALUES(".
				$this->db->escape($this->vi_name).", " .
				$this->db->escape($this->en_name).", " .
				$this->db->escape($this->link_to).", " .
				$this->db->escape($this->link_type).", " .
				//$this->db->escape($this->position).", " .
				intval($this->order_view).", " .
				intval($this->active).", " .
				$this->db->escape($this->image).
				")";
		
		$this->db->query($sql); 
		
		if($this->db->affected_rows()){
			
			$this->software_id = $this->db->insert_id(); 
			
			$this->order_view = $this->software_id;
			
			$this->update($this);
			
			return $this->software_id;
			
		}
		
		return 0;
	}
	
	function update($adv){
		$sql = "UPDATE gtb_software
				SET ".
				"vi_name = " .  $this->db->escape($adv->vi_name) . "," .
				"en_name = " .  $this->db->escape($adv->en_name) . "," .
				"link_to = " .  $this->db->escape($adv->link_to) . ", " .
				"link_type = " .  $this->db->escape($adv->link_type) . ", " .
				"order_view = " .  intval($adv->order_view) . ", " .
				"active = " .  intval($adv->active)  . ", " .
				"image = " .  $this->db->escape($adv->image) .
				"WHERE software_id = " .intval($adv->software_id);
		$this->db->query($sql);
		return $this->db->affected_rows();
	}
	
	function delete($id){
		if(!is_array($id)) $id = array($id);
		$ids = trim(implode(',', $id));
		if($ids=='') return;
		
		$sql = "SELECT image FROM gtb_software WHERE software_id IN ($ids)";
		$query = $this->db->query($sql);
		$result = $query->result();
		
		if(count($result) > 0){
			for($i = 0; $i < count($result); $i++){
				if($result[$i]->image!=''){
					@unlink($result[$i]->image);
				}
			}
		}
				
		$sql = "DELETE FROM gtb_adv WHERE id_adv IN ($ids)";
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function getAll($order_view = '', $offset = 0, $limit=0){
		$result = null;
		if($limit)
			$sql = "SELECT a.*,".
			 		" (SELECT MAX(t3.order_view) FROM gtb_adv t3) as max_order, 
					(SELECT MIN(t4.order_view) FROM gtb_adv t4) as min_order, 
					(SELECT (t5.id_adv) FROM gtb_adv t5 WHERE t5.order_view > a.order_view LIMIT 0,1) as id_next,
					(SELECT (t6.id_adv) FROM gtb_adv t6 WHERE t6.order_view < a.order_view ORDER BY t6.order_view DESC LIMIT 0,1) as id_preview " .
					" FROM gtb_adv a".
					" ORDER BY a.order_view " .  $order_view . 
				 	" LIMIT " . intval($offset) . ", " . intval($limit);
		else
			$sql = "SELECT a.*".
					" FROM gtb_adv a".
					" WHERE a.active = 1".
					" ORDER BY a.order_view " .  $order_view ;
		//echo $sql;	
		$query = $this->db->query($sql);
		$result = $query->result();
      
		return $result;
	}
	
	function getAllCount(){
		$sql = 'SELECT COUNT(*) as num_record
				FROM gtb_adv a';
		
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->num_record;
		}
       
		return 0;
		
	}
	
	function getCenterAdv(){
		$sql = 'SELECT name, link_to, image
				FROM gtb_adv			
				WHERE position = "center" AND active = 1 ORDER BY order_view DESC LIMIT 0,1';
		$query = $this->db->query($sql);
		      
		if($result = $query->result()){
			return $result[0];
		}
       
		return null;
	}
		
	function getMaxOrder(){
		
		$sql = "SELECT MAX(order_view) as order_max
				FROM gtb_adv ";
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->order_max ;
		}
       
		return 0;
	}
	
	function getById($id_adv){
		$sql = 'SELECT *
				FROM gtb_adv a
				WHERE a.id_adv = ' . intval($id_adv);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}
       
		return null;
	}
	
	
		
//	
}

// EOF
